/**
 * Copyright © 2024 北京迅安网络系统有限责任公司 Co., Ltd. All rights reserved.
 */

#include "osid_manager_proxy.h"
#include "hilog_util.h"

namespace OHOS {
namespace OSID {
namespace osidService {

bool OsidManagerProxy::VerifyApk(std::string path)
{
    MessageParcel data;
    MessageParcel reply;
    MessageOption option(MessageOption::TF_SYNC);
    bool result = false;
    std::string OSID_INTERFACE_DESCRIPTOR = "osid.IOsidManager";
    data.WriteString16(Str8ToStr16(OSID_INTERFACE_DESCRIPTOR));
    data.WriteString16(Str8ToStr16(path));
    int32_t st = Remote()->SendRequest(IOsidManager::CMD_VERIFY_APK, data, reply, option);
    if (st != ERR_NONE) {
        HILOGW("VerifyApk failed,error code is %{public}d", st);
        return result;
    }

    result = reply.ReadBool();
    return result;
}
std::string OsidManagerProxy::CurlTest()
{
    MessageParcel data;
    MessageParcel reply;
    MessageOption option(MessageOption::TF_SYNC);
    std::string result = "error";
    std::string OSID_INTERFACE_DESCRIPTOR = "osid.IOsidManager";
    data.WriteString16(Str8ToStr16(OSID_INTERFACE_DESCRIPTOR));
    int32_t st = Remote()->SendRequest(IOsidManager::CMD_CURL_TEST, data, reply, option);
    if (st != ERR_NONE) {
        HILOGW("CurlTest failed,error code is %{public}d", st);
        return result;
    }

    // is exit ReadString() function?
    result = reply.ReadString();
    return result;
}
bool OsidManagerProxy::friGetPmsActiveState()
{
    MessageParcel data;
    MessageParcel reply;
    MessageOption option(MessageOption::TF_SYNC);
    bool result = false;
    std::string OSID_INTERFACE_DESCRIPTOR = "osid.IOsidManager";
    data.WriteString16(Str8ToStr16(OSID_INTERFACE_DESCRIPTOR));
    int32_t st = Remote()->SendRequest(IOsidManager::CMD_GET_PMS_ACTIVE_STATE, data, reply, option);
    if (st != ERR_NONE) {
        HILOGW("friGetPmsActiveState failed,error code is %{public}d", st);
        return result;
    }
    result = reply.ReadBool();
    return result;
}

std::string OsidManagerProxy::friGetDeviceOSID()
{
    MessageParcel data;
    MessageParcel reply;
    MessageOption option(MessageOption::TF_SYNC);
    std::string result = "error";
    std::string OSID_INTERFACE_DESCRIPTOR = "osid.IOsidManager";
    data.WriteString16(Str8ToStr16(OSID_INTERFACE_DESCRIPTOR));
    int32_t st = Remote()->SendRequest(IOsidManager::CMD_GET_DEVICE_OSID, data, reply, option);
    if (st != ERR_NONE) {
        HILOGW("friGetDeviceOSID failed,error code is %{public}d", st);
        return result;
    }
    result = reply.ReadString();
    return result;
}

std::string OsidManagerProxy::friGetDevicePMSID()
{
    MessageParcel data;
    MessageParcel reply;
    MessageOption option(MessageOption::TF_SYNC);
    std::string result = "error";
    std::string OSID_INTERFACE_DESCRIPTOR = "osid.IOsidManager";
    data.WriteString16(Str8ToStr16(OSID_INTERFACE_DESCRIPTOR));
    int32_t st = Remote()->SendRequest(IOsidManager::CMD_GET_DEVICE_PMSID, data, reply, option);
    if (st != ERR_NONE) {
        HILOGW("friGetDeviceOSID failed,error code is %{public}d", st);
        return result;
    }
    result = reply.ReadString();
    return result;
}

bool OsidManagerProxy::friCheckOSID(std::string checkOSID)
{
    MessageParcel data;
    MessageParcel reply;
    MessageOption option(MessageOption::TF_SYNC);
    bool result = false;
    std::string OSID_INTERFACE_DESCRIPTOR = "osid.IOsidManager";
    data.WriteString16(Str8ToStr16(OSID_INTERFACE_DESCRIPTOR));
    data.WriteString16(Str8ToStr16(checkOSID));
    int32_t st = Remote()->SendRequest(IOsidManager::CMD_CHECK_OSID, data, reply, option);
    if (st != ERR_NONE) {
        HILOGW("friCheckOSID failed,error code is %{public}d", st);
        return result;
    }

    result = reply.ReadBool();
    return result;
}

bool OsidManagerProxy::friCheckPMSID(std::string checkPMSID)
{
    MessageParcel data;
    MessageParcel reply;
    MessageOption option(MessageOption::TF_SYNC);
    bool result = false;
    std::string OSID_INTERFACE_DESCRIPTOR = "osid.IOsidManager";
    data.WriteString16(Str8ToStr16(OSID_INTERFACE_DESCRIPTOR));
    data.WriteString16(Str8ToStr16(checkPMSID));
    int32_t st = Remote()->SendRequest(IOsidManager::CMD_CHECK_PMSID, data, reply, option);
    if (st != ERR_NONE) {
        HILOGW("friCheckPMSID failed,error code is %{public}d", st);
        return result;
    }

    result = reply.ReadBool();
    return result;
}

std::string OsidManagerProxy::friGetRealCert()
{
    MessageParcel data;
    MessageParcel reply;
    MessageOption option(MessageOption::TF_SYNC);
    std::string result = "error";
    std::string OSID_INTERFACE_DESCRIPTOR = "osid.IOsidManager";
    data.WriteString16(Str8ToStr16(OSID_INTERFACE_DESCRIPTOR));
    int32_t st = Remote()->SendRequest(IOsidManager::CMD_GET_REAL_CERT, data, reply, option);
    if (st != ERR_NONE) {
        HILOGW("friGetDeviceOSID failed,error code is %{public}d", st);
        return result;
    }
    result = reply.ReadString();
    return result;
}
} // namespace osidService
} // namespace OSID
} // namespace OHOS